package cn.stylefeng.guns.modular.business.frequency.orderfinish.service.impl;

import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.modular.business.custom.enums.BonusTypeEnum;
import cn.stylefeng.guns.modular.business.custom.enums.OrderStatusEnum;
import cn.stylefeng.guns.modular.business.frequency.banner.config.activityMq.Producer;
import cn.stylefeng.guns.modular.business.frequency.finance.mapper.FinanceMapper;
import cn.stylefeng.guns.modular.business.frequency.finance.model.result.PkgFinance;
import cn.stylefeng.guns.modular.business.frequency.financetransport.mapper.FinanceTransportMapper;
import cn.stylefeng.guns.modular.business.frequency.ordercarverify.mapper.OrderCarVerifyMapper;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.mapper.OrderExtraFeeMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.entity.OrderFinish;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.mapper.OrderFinishMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.*;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.service.OrderFinishService;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.entity.OrderFinishImage;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.mapper.OrderFinishImageMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecord.entity.OrderFinishRecord;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecord.mapper.OrderFinishRecordMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.entity.OrderFinishRecordDetail;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.mapper.OrderFinishRecordDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.param.ErrorMsg;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.mapper.OrderPayRecordMapper;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.entity.OrderRoyalty;
import cn.stylefeng.guns.modular.business.frequency.orderroyalty.mapper.OrderRoyaltyMapper;
import cn.stylefeng.guns.modular.business.frequency.ordersub.mapper.OrderSubMapper;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderDTO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOnlinePayRecord;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderFinishRecord;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderSub;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.entity.OrderTransportRelation;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.mapper.OrderTransportRelationMapper;
import cn.stylefeng.guns.modular.business.frequency.statistics.PkgOrderFinish;
import cn.stylefeng.guns.modular.business.frequency.transport.mapper.TransportMapper;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.entity.TransportLevel;
import cn.stylefeng.guns.modular.business.frequency.transportlevel.mapper.TransportLevelMapper;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.entity.TransportLevelDetail;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.mapper.TransportLevelDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.transportsign.enums.TransportStatusEnum;
import cn.stylefeng.guns.modular.business.timer.delay.DelayQueueManager;
import cn.stylefeng.guns.modular.util.BaseUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单完成确认表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderFinishServiceImpl extends ServiceImpl<OrderFinishMapper, OrderFinish> implements OrderFinishService {


    @Resource
    private Producer producer;
    @Resource
    private OrderSubMapper orderSubMapper;
    @Resource
    private OrderFinishRecordMapper orderFinishRecordMapper;
    @Resource
    private OrderFinishRecordDetailMapper orderFinishRecordDetailMapper;
    @Resource
    private OrderFinishImageMapper orderFinishImageMapper;
    @Resource
    private FinanceMapper financeMapper;
    @Resource
    private FinanceTransportMapper financeTransportMapper;
    @Resource
    private OrderFinishMapper orderFinishMapper;
    @Resource
    private OrderExtraFeeMapper orderExtraFeeMapper;
    @Resource
    private OrderTransportRelationMapper orderTransportRelationMapper;
    @Resource
    private TransportLevelDetailMapper transportLevelDetailMapper;
    @Resource
    private TransportMapper transportMapper;
    @Resource
    private TransportLevelMapper transportLevelMapper;
    @Resource
    private OrderRoyaltyMapper orderRoyaltyMapper;
    @Resource
    private OrderPayRecordMapper orderPayRecordMapper;
    @Resource
    private OrderCarVerifyMapper orderCarVerifyMapper;
    @Resource
    private OrderSubMapper orderSubV2Mapper;
    @Resource
    private DelayQueueManager delayQueueManager;


    @Override
    public boolean checkFinishAdd(OrderFinishRecordVO orderFinishRecordVO, ErrorMsg errorMsg) {
        if (orderFinishRecordVO == null || orderFinishRecordVO.getOrderIdList() == null || orderFinishRecordVO.getOrderIdList()
                .size() == 0) {
            errorMsg.setErrorMsg("交付信息错误！");
            return false;
        }
        // 订单状态是否可以交付
        List<Integer> orderStatusList = orderSubV2Mapper.getOrderStatusByOrderIds(orderFinishRecordVO.getOrderIdList());
        if (!BaseUtils.listIsAll(orderStatusList, OrderStatusEnum.WAIT_TAKE.status)) {
            errorMsg.setErrorMsg("当前订单状态可不交付，可能已在其他设备交付完成");
            return false;
        }
        // 有无到付金额
        BigDecimal totalDeliveryFee = orderSubV2Mapper.getTotalDeliveryFee(orderFinishRecordVO.getOrderIdList());
        if (totalDeliveryFee == null || totalDeliveryFee.compareTo(BigDecimal.ZERO) == 0) {
            if (orderFinishRecordVO.getType() != null) {
                errorMsg.setErrorMsg("参数错误，非到付订单不可选择支付方式！");
                return false;
            }
        }
        return true;
    }


    @Override
    @Transactional
    public void finish(Long userId, OrderFinishRecordVO orderFinishRecordVO) {
        OrderFinishRecord orderFinishRecord = new OrderFinishRecord();
       BeanUtils.copyProperties(orderFinishRecordVO, orderFinishRecord);
        orderFinishRecord.setCreateUser(userId);
        orderFinishRecordMapper.insert(orderFinishRecord);
        // 交付图片保存
        if (orderFinishRecordVO.getTakeCarImageList() != null && orderFinishRecordVO.getTakeCarImageList().size() > 0) {
            for (String url : orderFinishRecordVO.getTakeCarImageList()) {
                OrderFinishImage ofi = new OrderFinishImage();
                ofi.setFinishId(orderFinishRecord.getId());
                ofi.setUrl(url);
                ofi.setType(1);
                orderFinishImageMapper.insert(ofi);
            }
        }
        if (orderFinishRecordVO.getPaymentImageList() != null && orderFinishRecordVO.getPaymentImageList().size() > 0) {
            for (String url : orderFinishRecordVO.getPaymentImageList()) {
                OrderFinishImage ofi = new OrderFinishImage();
                ofi.setFinishId(orderFinishRecord.getId());
                ofi.setUrl(url);
                ofi.setType(2);
                orderFinishImageMapper.insert(ofi);
            }
        }
        // 交付记录订单表插入
        for (Long orderId : orderFinishRecordVO.getOrderIdList()) {
            OrderFinishRecordDetail ofrd = new OrderFinishRecordDetail();
            ofrd.setFinishRecordId(orderFinishRecord.getId());
            ofrd.setOrderId(Long.valueOf(orderId));
            orderFinishRecordDetailMapper.insert(ofrd);
            // OrderFinish处理
            OrderFinish orderFinish = new OrderFinish();
           BeanUtils.copyProperties(orderFinishRecordVO, orderFinish);
            orderFinish.setOrderId(Long.valueOf(orderId));
            orderFinish.setCreateUser(orderFinishRecord.getCreateUser());
            // 计算营业额，毛利数据
            PkgFinanceGross orderGross = financeMapper.getFinanceGrossByOrderId(orderId);
            orderFinish.setCollectionFee(orderGross.getTotalCollection());
            orderFinish.setPaymentFee(orderGross.getTotalPayment());
            orderFinish.setGrossFee(orderGross.getGross());
            orderFinishMapper.insert(orderFinish);
            // 转账
            if (orderFinish.getType() != null && orderFinish.getType() == 2) {
                if (orderFinishRecordVO.getPaymentImageList() != null && orderFinishRecordVO.getPaymentImageList()
                        .size() > 0) {
                    // 转账凭证
                    financeMapper.updateHasTransferVoucher(orderFinish.getOrderId());
                }
            }
            // 代收更新到付结算人
            /*if (orderFinishRecordVO.getType() != null && orderFinishRecordVO.getType() == 4) {
                financeMapper.updateDeliveryInfo(orderId, orderFinishRecordVO.getCollectionName(), orderFinishRecordVO.getCollectionPhone());
                financeMapper.updateDeliveryHasCollection(orderId);
                Transport orderLastTransport = transportMapper.getOrderLastTransport(orderId);
                financeDetailMapper.updateSettlementUserInfo(orderId, FinanceTypeEnum.DELIVERY.type, orderFinishRecordVO.getCollectionName(),
                        orderFinishRecordVO.getCollectionPhone(), orderLastTransport.getSettlementId());
            }*/
            // 子订单状态
            orderSubMapper.updateStatusById(orderFinish.getOrderId()
                    , OrderStatusEnum.CUSTOMER_HAS_SIGN.status, OrderStatusEnum.WAIT_TAKE.status);
            // 运单状态修改
            // 如果运单状态为运输中，切该运单下所有直交客户的订单都已完成，则运单状态变为已签收
            // 最后一段承运
            OrderTransportRelation otr = orderTransportRelationMapper.selectOne(new QueryWrapper<OrderTransportRelation>().lambda()
                    .eq(OrderTransportRelation::getOrderId, orderFinish.getOrderId())
                    .last("order by transport_id desc limit 1"));
            // 运单状态
            int unDeliveryCount = transportLevelDetailMapper.countUnDeliveryCount(otr.getTransportId(), orderFinish.getOrderId());
            int unSignedCount = transportLevelDetailMapper.countUnSignedCount(otr.getTransportId(), orderFinish.getOrderId());
            if (unDeliveryCount == 0 && unSignedCount == 0) {
                transportMapper.updateStatusById(otr.getTransportId(), TransportStatusEnum.SIGN_FINISHED.status, null);
            } else if (unDeliveryCount == 0 && unSignedCount > 0) {
                transportMapper.updateStatusById(otr.getTransportId(), TransportStatusEnum.TRANSPORTING.status, null);
            }

            // ----------2021.08.24修改--------
            TransportLevelDetail lastLevelDetail = transportLevelDetailMapper.getLastTransportLevelDetail(orderFinish.getOrderId());
            List<TransportLevelDetail> levelDetailList = transportLevelDetailMapper.selectList(new QueryWrapper<TransportLevelDetail>().lambda()
                    .eq(TransportLevelDetail::getLevelId, lastLevelDetail.getLevelId()));
            List<Long> orderIds = levelDetailList.stream()
                    .map(transportLevelDetail -> transportLevelDetail.getOrderId())
                    .collect(Collectors.toList());
            List<Integer> orderStatusList = orderSubV2Mapper.getOrderStatusByOrderIds(orderIds);
            if (BaseUtils.listIsAll(orderStatusList, OrderStatusEnum.CUSTOMER_HAS_SIGN.status)) {
                // 分段已到达状态修改
                transportLevelMapper.updateArrivedStatus(lastLevelDetail.getLevelId());
            }
            // 财务应付状态修改
            changeFinancePaymentStatus(orderId);
            // ----------2021.08.24------------

            // 司机端送车任务状态更新--已完成
            //supplierOrderMapper.updateStatusByOrderIdAndType(orderId, 2, 3);
            // activemq通知下游系统计算奖金
            producer.sendBonus(BonusTypeEnum.DELIVERY.type, orderFinish.getOrderId(), userId, null);
            // 提成表
            OrderRoyalty orderRoyalty = new OrderRoyalty();
            orderRoyalty.setOrderId(orderFinish.getOrderId());
            orderRoyalty.setGrossFee(orderGross.getGross());
            BigDecimal royaltyFee = orderGross.getGross().subtract(new BigDecimal("40")).multiply(new BigDecimal("0.5"))
                    .compareTo(BigDecimal.ZERO) > 0 ?
                    orderGross.getGross().subtract(new BigDecimal("40"))
                            .multiply(new BigDecimal("0.5")) : BigDecimal.ZERO;
            orderRoyalty.setRoyaltyFee(royaltyFee);
            orderRoyaltyMapper.update(orderRoyalty, new QueryWrapper<OrderRoyalty>().lambda()
                    .eq(OrderRoyalty::getOrderId, orderFinish.getOrderId()));
            // 交付完成，订单确认到达延时队列
            OrderSubDelayVO osdv = new OrderSubDelayVO(Long.valueOf(orderId), new Date());
            delayQueueManager.addToDelayQueue(osdv);
        }
    }

    private void changeFinancePaymentStatus(Long orderId) {
        PkgFinance pkgFinance = financeMapper.getPkgFinanceByOrderId(Long.valueOf(orderId));
        if (pkgFinance != null) {
            boolean totalHasPay = false;
            boolean transportHasPay = false;
            boolean otherHasPay = false;
            if (pkgFinance.getTotalUnPayment().compareTo(BigDecimal.ZERO) == 0) {
                totalHasPay = true;
            }
            // 未付承运费
            BigDecimal unPayTransportFee = financeTransportMapper.getUnPayTransportFeeByOrderId(Long.valueOf(orderId));
            if (unPayTransportFee.compareTo(BigDecimal.ZERO) == 0) {
                transportHasPay = true;
            }
            // 未付其他费用
            BigDecimal unPayOtherFee = orderExtraFeeMapper.getUnPayOtherFeeByOrderId(Long.valueOf(orderId));
            if (unPayOtherFee.compareTo(BigDecimal.ZERO) == 0) {
                otherHasPay = true;
            }
            // 如果订单应付款已经全部付完，则修改应付状态
            if (totalHasPay && transportHasPay && otherHasPay) {
                financeMapper.updatePaymentStatus(2, Long.valueOf(orderId));
            }
        }
    }

    @Override
    public Page<PkgOrderFinish> getFinishOverListPage(Page page, FinishRecordDTO orderDTO) {
        return orderFinishRecordMapper.getFinishOverListPage(page, orderDTO);
    }

    @Override
    public PkgOrderFinishRecord getPkgOrderFinishRecordDetail(String id) {
        PkgOrderFinishRecord pkgOrderFinishRecord = orderFinishRecordMapper.getPkgOrderFinishRecordDetail(id);
        // 线上支付信息
        if (pkgOrderFinishRecord.getType() != null && pkgOrderFinishRecord.getType() == 1 && StringUtils.isNotBlank(pkgOrderFinishRecord.getClientSn())) {
            PkgOnlinePayRecord onlinePayRecord = orderPayRecordMapper.getSuccessRecordByClientSn(pkgOrderFinishRecord.getClientSn());
            pkgOrderFinishRecord.setOnlinePayRecord(onlinePayRecord);
        }
        // 交付图片
        List<String> takeCarImageList = orderFinishImageMapper.getImageByIdAndType(pkgOrderFinishRecord.getId(), 1);
        pkgOrderFinishRecord.setTakeCarImageList(takeCarImageList);
        List<String> paymentImageList = orderFinishImageMapper.getImageByIdAndType(pkgOrderFinishRecord.getId(), 2);
        pkgOrderFinishRecord.setPaymentImageList(paymentImageList);
        List<Long> orderIdList = orderFinishRecordDetailMapper.getOrderIdListByFinishRecordId(pkgOrderFinishRecord.getId());
        // 子订单详情
        List<PkgOrderSub> list = new ArrayList<>();
        for (Long orderId : orderIdList) {
            PkgOrderSub pkgOrderSub = orderSubMapper.getSubOrderDetail(orderId);
            pkgOrderSub.setOrderCarVerify(orderCarVerifyMapper.getPkgOrderCarVerify(orderId));
            list.add(pkgOrderSub);
        }
        pkgOrderFinishRecord.setOrderSubList(list);
        return pkgOrderFinishRecord;
    }

    @Override
    public List<PkgOrderSub> getPkgOrderFinishRecordDetail(FinishRecordContractVO finishRecordContractVO) {
        if (finishRecordContractVO != null && StringUtils.isNotBlank(finishRecordContractVO.getFinishRecordId())) {
            List<Long> orderIdList = orderFinishRecordDetailMapper.getOrderIdListByFinishRecordId(finishRecordContractVO.getFinishRecordId());
            // 子订单详情
            List<PkgOrderSub> list = new ArrayList<>();
            for (Long orderId : orderIdList) {
                PkgOrderSub pkgOrderSub = orderSubV2Mapper.getOrderDetail(orderId);
                pkgOrderSub.setOrderCarVerify(orderCarVerifyMapper.getPkgOrderCarVerify(orderId));
                list.add(pkgOrderSub);
            }
            return list;
        } else {
            // 子订单详情
            List<PkgOrderSub> list = new ArrayList<>();
            for (String orderId : finishRecordContractVO.getOrderIdList()) {
                PkgOrderSub pkgOrderSub = orderSubV2Mapper.getOrderDetail(Long.valueOf(orderId));
                pkgOrderSub.setOrderCarVerify(orderCarVerifyMapper.getPkgOrderCarVerify(Long.valueOf(orderId)));
                list.add(pkgOrderSub);
            }
            return list;
        }
    }

    @Override
    public Page getWaitFinishListPage(Page page, OrderDTO orderDTO) {
        Page<PkgOrderFinish> list = orderFinishMapper.getWaitFinishListPage(page, orderDTO);
        return list;
    }


    @Override
    public boolean checkLevelFinish(OrderFinishRecordVO orderFinishRecordVO, StringBuilder errorMsg) {
        if (orderFinishRecordVO == null || orderFinishRecordVO.getLevelId()==null) {
            errorMsg.append("参数错误");
            return false;
        }
        TransportLevel level = transportLevelMapper.selectById(orderFinishRecordVO.getLevelId());
        if (level.getHasArrived() == 1) {
            errorMsg.append("当前分段已经交付完成！");
            return false;
        }
        List<Long> orderId = transportLevelDetailMapper.selectList(new QueryWrapper<TransportLevelDetail>()
                        .lambda().eq(TransportLevelDetail::getLevelId, orderFinishRecordVO.getLevelId()))
                .stream().map(t-> t.getOrderId()).collect(Collectors.toList());

        BigDecimal totalDeliveryFee = orderSubV2Mapper.getTotalDeliveryFee(orderId);
        if (totalDeliveryFee == null || totalDeliveryFee.compareTo(BigDecimal.ZERO) == 0) {
            if (orderFinishRecordVO.getType() != null) {
                errorMsg.append("参数错误，非到付订单不可选择支付方式！");
                return false;
            }
        }
        // 默认
        orderFinishRecordVO.setConsigneeName(level.getReceiveUser());
        orderFinishRecordVO.setConsigneePhone(level.getReceivePhone());
        return true;
    }

    @Override
    public void levelFinish(OrderFinishRecordVO orderFinishRecordVO) {
        OrderFinishRecord orderFinishRecord = new OrderFinishRecord();
        orderFinishRecord.setRemark(orderFinishRecordVO.getRemark());
        orderFinishRecord.setConsigneeName(orderFinishRecordVO.getConsigneeName());
        orderFinishRecord.setConsigneePhone(orderFinishRecordVO.getConsigneePhone());
        orderFinishRecord.setLogisticsFinish(1);
        orderFinishRecord.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        orderFinishRecordMapper.insert(orderFinishRecord);
        // 交付图片保存
        if (orderFinishRecordVO.getTakeCarImageList() != null && orderFinishRecordVO.getTakeCarImageList().size() > 0) {
            for (String url : orderFinishRecordVO.getTakeCarImageList()) {
                OrderFinishImage ofi = new OrderFinishImage();
                ofi.setFinishId(orderFinishRecord.getId());
                ofi.setUrl(url);
                ofi.setType(1);
                orderFinishImageMapper.insert(ofi);
            }
        }
        List<Long> orderIds = transportLevelDetailMapper.getOrderIdListByLevelId(orderFinishRecordVO.getLevelId());
        List<Long> list = new ArrayList<>();
        // 老数据兼容
        if (orderIds != null && orderIds.size() > 0) {
            for (Long orderId : orderIds) {
                int status = orderSubV2Mapper.getStatusById(Long.valueOf(orderId));
                if (status == 6) {
                    list.add(orderId);
                }
            }
        }
        // 交付记录订单表插入
        for (Long orderId : list) {
            OrderFinishRecordDetail ofrd = new OrderFinishRecordDetail();
            ofrd.setFinishRecordId(orderFinishRecord.getId());
            ofrd.setOrderId(Long.valueOf(orderId));
            orderFinishRecordDetailMapper.insert(ofrd);
            // OrderFinish处理
            OrderFinish orderFinish = new OrderFinish();
            BeanUtils.copyProperties(orderFinishRecordVO, orderFinish);
            orderFinish.setOrderId(Long.valueOf(orderId));
            orderFinish.setCreateUser(orderFinishRecord.getCreateUser());
            // 计算营业额，毛利数据
            PkgFinanceGross orderGross = financeMapper.getFinanceGrossByOrderId(orderId);
            orderFinish.setCollectionFee(orderGross.getTotalCollection());
            orderFinish.setPaymentFee(orderGross.getTotalPayment());
            orderFinish.setGrossFee(orderGross.getGross());
            orderFinishMapper.insert(orderFinish);
            // 子订单状态
            orderSubMapper.updateStatusById(orderFinish.getOrderId(), OrderStatusEnum.CUSTOMER_HAS_SIGN.status, OrderStatusEnum.WAIT_TAKE.status);
            // 财务应付状态修改
            changeFinancePaymentStatus(orderId);
            // ----------2021.08.24------------

            // 司机端送车任务状态更新--已完成
            //supplierOrderMapper.updateStatusByOrderIdAndType(orderId, 2, 3);
            // activemq通知下游系统计算奖金
            producer.sendBonus(BonusTypeEnum.DELIVERY.type, orderFinish.getOrderId(), LoginContextHolder.me()
                    .getSysLoginUserId(), null);
            // 提成表
            OrderRoyalty orderRoyalty = new OrderRoyalty();
            orderRoyalty.setOrderId(orderFinish.getOrderId());
            orderRoyalty.setGrossFee(orderGross.getGross());
            BigDecimal royaltyFee = orderGross.getGross().subtract(new BigDecimal("40")).multiply(new BigDecimal("0.5"))
                    .compareTo(BigDecimal.ZERO) > 0 ?
                    orderGross.getGross().subtract(new BigDecimal("40"))
                            .multiply(new BigDecimal("0.5")) : BigDecimal.ZERO;
            orderRoyalty.setRoyaltyFee(royaltyFee);
            orderRoyaltyMapper.update(orderRoyalty, new QueryWrapper<OrderRoyalty>().lambda()
                    .eq(OrderRoyalty::getOrderId, orderFinish.getOrderId()));
            // 交付完成，订单确认到达延时队列
            OrderSubDelayVO osdv = new OrderSubDelayVO(orderId, new Date());
            delayQueueManager.addToDelayQueue(osdv);
        }
        // 运单状态修改
        TransportLevel level = transportLevelMapper.selectById(orderFinishRecordVO.getLevelId());
        // 如果运单状态为运输中，切该运单下所有直交客户的订单都已完成，则运单状态变为已签收
        // 运单状态
        int unDeliveryCount = transportLevelMapper.countUnDeliveryCount(level.getTransportId().toString(), level.getId().toString());
        int unSignedCount = transportLevelMapper.countUnSignedCount(level.getTransportId().toString(), level.getId().toString());
        if (unDeliveryCount == 0 && unSignedCount == 0) {
            transportMapper.updateStatusById(level.getTransportId(), TransportStatusEnum.SIGN_FINISHED.status, null);
        } else if (unDeliveryCount == 0 && unSignedCount > 0) {
            transportMapper.updateStatusById(level.getTransportId(), TransportStatusEnum.TRANSPORTING.status, null);
        }
        // 分段已到达状态修改
        transportLevelMapper.updateArrivedStatus(Long.valueOf(orderFinishRecordVO.getLevelId()));
    }

    @Override
    public List<String> getOrderFinishPayImage(Long orderId) {
        PkgOrderFinishRecord pkgOrderFinishRecord = orderFinishRecordMapper.getPkgOrderFinishRecordDetailByOrderId(orderId);
        return orderFinishImageMapper.getImageByIdAndType(pkgOrderFinishRecord.getId(), 2);
    }


}